package com.simon.app.october.modules.sys.service.impl;

import com.simon.app.october.common.utils.Constant;
import com.simon.app.october.modules.sys.dao.SysMenuDao;
import com.simon.app.october.modules.sys.entity.SysMenuEntity;
import com.simon.app.october.modules.sys.service.SysMenuService;
import com.simon.app.october.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    SysUserService userService;

    @Autowired
    SysMenuDao menuDao;


    @Override
    public List<SysMenuEntity> getUserMenuList(long userId) {

        if (userId == Constant.SUPER_ADMIN) {
            return getAllMenuList(null);
        }

        // 获取用户的菜单列表
        List<Long> menuIdList = userService.getAllMenuId(userId);

        return getAllMenuList(menuIdList);
    }

    @Override
    public List<SysMenuEntity> getList() {
        return menuDao.getList();
    }

    @Override
    public List<SysMenuEntity> getNotButtonMenuList() {
        return menuDao.getNotButtonMenuList();
    }

    @Override
    public SysMenuEntity findById(long menuId) {
        return menuDao.findById(menuId);
    }

    @Override
    public void save(SysMenuEntity menuEntity) {
        menuDao.insert(menuEntity);
    }

    @Override
    public void update(SysMenuEntity menuEntity) {
        menuDao.update(menuEntity);
    }

    @Override
    public void delete(long menuId) {
        // 系统目录不能
        if (menuId < 31L) {

        }
        // 有子菜单不能删除

        // 删除
        menuDao.delete(menuId);
    }

    @Override
    public List<SysMenuEntity> getListByParentId(long menuId) {
        return menuDao.getListParentId(menuId);
    }

    private List<SysMenuEntity> getAllMenuList(List<Long> menuIdList) {
        // 查询跟菜单列表
        List<SysMenuEntity> menuList = getListParentId(0L, menuIdList);

        // 递归的获取子菜单
        return createMenuTree(menuList, menuIdList);
    }


    /**
     * 递归的生成一颗菜单树
     * */
    private List<SysMenuEntity> createMenuTree(List<SysMenuEntity> menuList, List<Long> menuIdList) {
        List<SysMenuEntity> menus = new ArrayList<>();

        for (SysMenuEntity menu : menuList) {
            if (Constant.MenuType.CATALOG.getValue() == menu.getType()) {
                menu.setList(createMenuTree(getListParentId(menu.getMenuId(), menuIdList), menuIdList));
            }
            menus.add(menu);
        }
        return menus;
    }

    private List<SysMenuEntity> getListParentId(Long parentId, List<Long> menuIdList) {
        List<SysMenuEntity> menuList = getListParentId(parentId);

        if (menuIdList == null) {
            return menuList;
        }

        List<SysMenuEntity> userMenuList = new ArrayList<>();

        for (SysMenuEntity menu : menuList) {
            if (menuIdList.contains(menu.getMenuId())) {
                userMenuList.add(menu);
            }
        }

        return userMenuList;
    }

    private List<SysMenuEntity> getListParentId(Long parentId) {
        return menuDao.getListParentId(parentId);
    }
}
